<?php
/**
 * Role.php
 * Created by RisingSun
 * User: MarkSpace 
 * Create on 2023/10/9
 */
namespace common;

use Mscore\Core\Db;

class Role extends Db
{
    /** @var self $instance 存放实例 */
    static public $instance;

    /** @var User $user 用户信息 */
    private $user;
    /** @var pertable $pertable 权限表信息 */
    static private $rules='sys_rules';
    static private $rolehas='sys_role_has';


    /**
     * 公有化获取实例方法
     * @return Auth
     * @throws getInstance
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof Auth)) {
            self::$instance = new self;
        }
        return self::$instance;
    }
    
    /**
     * 权限-获取权限操作
     * @param array $getRules 
     * @param array $user 
     */ 
    public function getRules($user){
        //user为空，直接终止执行
        if(empty($user['grade'])){return;}
        $where = 'a.status >0 and a.dels<1';
        switch (true) {
          case $user['grade']=='superadmin':
            $datalist = Db::name(self::$rules)
                ->alias('a')
                ->field('a.id,a.title,a.code,a.url,a.icon,a.pid,a.is_top,a.langkey' )
                ->where($where)
                ->order('a.orderid asc, a.id asc')
                ->select()
                ->toArray();
            break;
          
          default:
            $datalist = Db::name(self::$rolehas)
                ->alias('b')
                ->join(self::$rules.' a','a.id=b.rules_id and b.role_id='.$user['roletype'],'left')
                ->field('a.id,a.title,a.code,a.url,a.icon,a.pid,a.is_top,a.langkey')
                ->where($where)
                ->order('a.orderid asc, a.id asc')
                ->select()
                ->toArray();
            break;
        }

        return $datalist;
    }

    /**
     * 权限操作-角色验证
     * @param array $checkRolePer 
     * @param array $user 
     */ 
    public function checkRolePer($module_id,$user){
        if(empty($user['grade'])){return;}
        if($user['grade']=='superadmin'){
            $datainfo = Db::name(self::$rules)
                    ->field('id,code,url,operate')
                    ->where('id='.$module_id)
                    ->find(); 
            $datalist=array_filter(json_decode(stripslashes($datainfo['operate']),true));
            $data['id']=$datainfo['id'];
            $data['code']=$datainfo['code'];
            $data['url']=$datainfo['url'];
            foreach ($datalist as $key => $value) {
                $data[$value['key']]=$value['url'];
            }
            unset($datalist);
        }else{
            $data = Db::name(self::$rolehas)
                    ->where('role_id =1 and rules_id='.$module_id)
                    ->value('operate'); 
            $data=array_filter(json_decode(stripslashes($data),true));
            return $data;
        }    
    }

    /**
     * 权限操作-角色获取
     * @param array $getRolePerAll 
     * @param array $user 
     */ 
    public function getRolePerAll($user){
        if(empty($user['grade'])){return;}
        if($user['grade']=='superadmin'){
            $data = Db::name(self::$rules)
                    ->field('id,code,url,operate')
                    ->select()
                    ->toArray();
            foreach ($data as $key => $value) {
                $data[$key]['url']=$value['code'].$value['url'];
                $data[$key]['operate']=json_decode(stripslashes($value['operate']),true);
                if(!empty($data[$key]['operate'])){
                  foreach ($data[$key]['operate'] as $k => $v){
                    $data[$key][$v['key']]=$v['url'];
                    $data[$key]['param'][$v['key']]=$v['value'];
                    $data[$key]['name'][$v['key']]=$v['name'];
                  }
                }
                unset($data[$key]['operate']);
            }
        }else{
            $data = Db::name(self::$rolehas)
                    ->field('operate')
                    ->where('role_id = '.$user['roletype'])
                    ->select()
                    ->toArray(); 
            foreach ($data as $key => $value) {
                $data[$key]=[];
                if(!empty($value['operate'])){
                   $data[$key]=json_decode(stripslashes($value['operate']),true); 
                }
            }
        }
        return array_values(array_filter($data));
    }

}